<?php

class Database {

	var $aTables;
	var $aForeignKeys;
	
	function Database () {
		$this->aTables      = array();
		$this->aForeignKeys = array();
	}

	function & GetTable ( $sName ) {
		$oTable = null;
		if ( $this->TableExists( $sName ) ) {
			$oTable =& $this->aTables[ strtoupper( $sName ) ];	
		} else {
			$oTable = $sName;	
		}
		return $oTable;
	}

	function HasTables() {
		return ! empty( $this->aTables );	
	}

	function GetTables() {
		return $this->aTables;
	}

	function GetTableNames() {
		return array_keys( $this->aTables );	
	}

	function GetForeignKeys() {
		return $this->aForeignKeys;
	}

	function AddTable ( &$oTable ) {
		$oTable->SetDatabase( $this );
		$this->aTables[ strtoupper( $oTable->GetName() ) ] =& $oTable;	
	}
	
	function TableExists( $sTableName ) {
		return ( array_key_exists( strtoupper( $sTableName ), $this->aTables ) );
	}
	
	function AddForeignKey( $sName, $sFromTable, $aColumnList, $sToTable, $bMandatory ) {
	
		if ( isset( $this->aForeignKeys[$sName] ) ) {
			return;	
		}
	
		$oFromTable =& $this->GetTable( $sFromTable );
		$oToTable   =& $this->GetTable( $sToTable   );
		
		if ( is_object( $oFromTable ) || is_object( $oToTable ) ) {
			$this->aForeignKeys[ $sName ] = new ForeignKey( $sName, $oFromTable, $aColumnList, $oToTable, $bMandatory );
		}	
	}
}

require_once( 'ForeignKey.php' );
?>
